package com.example.day0525.news

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.NewsBeanItem
import com.example.day0525.R
import com.example.day0525.databinding.ItemNewsBinding

class NewsAdapter:PagingDataAdapter<NewsBeanItem, NewsAdapter.NewsViewHolder>(holl) {
    private var mOnItemClickListener: OnItemClickListener? = null
    companion object{
        val holl =object :ItemCallback<NewsBeanItem>(){
            override fun areItemsTheSame(oldItem: NewsBeanItem, newItem: NewsBeanItem): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(oldItem: NewsBeanItem, newItem: NewsBeanItem): Boolean {
               return oldItem.id==newItem.id
            }
        }
    }

    class NewsViewHolder(view:View):ViewHolder(view){
        val  binding=DataBindingUtil.bind<ItemNewsBinding>(view)
        fun bind(news:NewsBeanItem){
            itemView.setOnClickListener {
                val newsId =news.id
            }
        }
    }


    override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
        holder.binding?.bean=getItem(position)
        val item = getItem(position)
        item?.let {
            holder.bind(it)
            val userId =it.id
        }
        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position,item!!)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_news, parent,false)
        return NewsViewHolder(view)
    }
     fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

}
interface OnItemClickListener {
    fun onItemClick(position: Int,item:NewsBeanItem)
}